package com.honglang.module.album.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.honglang.common.tools.Constants;
import com.honglang.common.tools.Pager;
import com.honglang.module.album.dao.AlbumDao;
import com.honglang.module.album.entity.Album;

public class AlbumServiceImpl implements AlbumService{
	
	private AlbumDao albumDao;
	private AlbumPhotoService albumPhotoService;
	
	
	
	/**
	 * save album
	 * @param album
	 * @return
	 */
	public Album save(Album album){
		return this.getAlbumDao().merge(album);
	}
	
	
	
	/**
	 * delete a album 
	 * @param album
	 */
	public void delete(Album album){
		 
		 this.getAlbumDao().delete(album);
	}
	
	
	
	/**
	 * delte a album
	 * @param albumId
	 */
	public void deleteAlbum(Integer albumId){
		this.getAlbumPhotoService().delete(this.getAlbumPhotoService().getAlbumPhotoByAlbumId(albumId));
		this.delete( this.getAlbumDao().getById(albumId));
	}
	
	
	
	/**
	 * batch delete album
	 * @param albumIds
	 */
	public void batchDeleteAlbum(String albumIds){
		List<Integer> idList = new ArrayList<Integer>();
		for(String id : albumIds.split(Constants.Album_Split)){
			idList.add(Integer.valueOf(id));
		}
		this.getAlbumPhotoService().delete(this.getAlbumPhotoService().getAlbumPhotoByAlbumIdList(idList));
		this.delete(this.getAlbumByIdList(idList));
	}
	
	
	/**
	 * get  a list of album by ids;
	 * @param idList
	 * @return
	 */
	public List<Album> getAlbumByIdList(List<Integer> idList){
		DetachedCriteria dc = DetachedCriteria.forClass(Album.class);
		dc.add(Restrictions.in("id", idList));
		return this.getAlbumDao().getByDetachedCriteria(dc);
	}
	
	
	
	/**
	 * batch delete album;
	 * @param albumList
	 */
	public void delete(List<Album> albumList){
		this.getAlbumDao().deleteAll(albumList);
	}
	
	
	
	
	/**
	 * get one page of album ;
	 * @param pageSize
	 * @param pageNo
	 * @return
	 */
	public Pager getPager(int pageSize,int pageNo){
		int rowCount = this.albumDao.getRowCountOfAllEntites();
		Pager pager = new Pager();
		pager.CreatePager(pageSize, pageNo, Constants.pageIndexCountPerPage, rowCount, this.albumDao.getEntitiesPerPage(pageSize, pageNo));
		return pager;
	}

	
	
	/**
	 * get album by id;
	 * @param albumId
	 * @return
	 */
	public Album getById(Integer albumId){
		return this.getAlbumDao().getById(albumId);
	}
	
	
	
	
	public AlbumDao getAlbumDao() {
		return albumDao;
	}

	public void setAlbumDao(AlbumDao albumDao) {
		this.albumDao = albumDao;
	}


	public AlbumPhotoService getAlbumPhotoService() {
		return albumPhotoService;
	}



	public void setAlbumPhotoService(AlbumPhotoService albumPhotoService) {
		this.albumPhotoService = albumPhotoService;
	}
	

}
